﻿//
//jFileControl
//
(function ($) {
    $.fn.SinglePictureControl = function (options) {
        //alert(this.attr('json_data'));
        var ranIndex = new Date().valueOf();
        var $this = this;
        if ($this.attr('hidden_btn') != 'False') {
            return;
        }
        var queueid = 'uploadqueue' + ranIndex;
        $this.append('<div id="' + queueid + '" class="uploadify-queue"></div>');

        $this.append('<a class="button" href="javascript:void(0)"><input id="upload' + ranIndex + '" class="uploadify" type="file"/></a>');


        try {
            var arr = $.parseJSON($this.attr('value_json'));
            $this.find('.image').attr('src', arr[0].FilePath);
        } catch (e) { }
        setTimeout(function () {
            var $img = $this.find('.image');
            if ($img.parent().height() > $img.height()) {
                $img.css("margin-top", (($img.parent().height() - $img.height()) / 2) + "px");
            } else {
                $img.css("margin-top", "0px");
                $img.parent().height($img.height());
            }

        }, 200);

        var fileUploadComplete = function (event, response) {
            var file = $.parseJSON(response).simple;
            var arr = [file];
            $this.find('.value_json').val($.toJSON([file]));
            var $img = $this.find('.image');
            $img.attr('src', file.FilePath);

            setTimeout(function () {
                if ($img.parent().height() > $img.height()) {
                    $img.css("margin-top", (($img.parent().height() - $img.height()) / 2) + "px");
                } else {
                    $img.css("margin-top", "0px");
                    $img.parent().height($img.height());
                }
            }, 200);
        }
        var options = {
            swf: '/SystemManage/javascripts/uploadify/uploadify.swf',
            uploader: '/SystemManage/Control/FileControl/Upload/Upload.aspx',
            queueID: queueid,
            height: 24,
            width: 50,
            buttonText: '上传',
            removeTimeout: 0.5,
            fileTypeDesc: $this.attr("file_desc"),
            fileTypeExts: $this.attr("file_filters"),
            fileObjName: $this.attr("name") || "file",
            auto: $this.attr("auto") || true,
            multi: false,
            formData: { User: $this.attr('user'), ajax: 1, cfgjson: BASE64.encoder($this.attr('upload_config')) },
            onUploadError: uploadifyError,
            //onUploadComplete: fileUploadComplete,
            onUploadSuccess: fileUploadComplete

        };


        $this.find('.uploadify').uploadify(options);


    }


})(jQuery);
